﻿using NewLife.Data;
using NewLife.Log;
using NewLife.Net.Handlers;
using NewLife.Net;
using NewLife;
using System;
using System.Collections.Generic;
using System.Linq;
using NewLife.Model;

namespace TK.Socket.ClientDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var list = new List<string> { "tcp://127.0.0.1:65001", "udp://127.0.0.1:65001" };
            TestClient(list[1]);
            Console.ReadKey();
        }

        static void TestClient(string host)
        {
            var uri = new NetUri(host);
            var client = uri.CreateRemote();
            XTrace.UseConsole(true, false);
            client.Log = XTrace.Log;
            client.LogSend = true;
            client.LogReceive = true;
            client.Received += (s, e) =>
            {
                var pk = e.Message as Packet;
                XTrace.WriteLine("收到：{0}", pk.ToStr());
            };
            client.Add(new LengthFieldCodec());
            client.Open();

            // 循环发送数据
            for (var i = 0; i < 5; i++)
            {
                var str = "你好" + (i + 1);
                var pk = new Packet(str.GetBytes());
                client.SendMessageAsync(pk);
            }
        }
    }
}
